﻿/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#ifndef _GCTL_PROGRESS_BAR_H
#define _GCTL_PROGRESS_BAR_H

#include "../core/exceptions.h"

#ifdef _WINDOWS
#include "windows.h"
#else
#include "sys/ioctl.h"
#endif

#include "cstring"
#include "iostream"
#include "iomanip"
#include "thread"
#include "chrono"

//进度条宏定义
#define TOTAL_PERCENTAGE 100.0 ///< 设置总的百分比为100%。
#define CHARACTER_WIDTH_PERCENTAGE 5 ///< 设置每显示一个字符所占的百分比为5%。

namespace gctl
{
	/**
	 * @brief      终端显示进度条的类。
	 * @warning    这个类是一个在终端显示进度的通用类型。
	 */
	class progress_bar
	{
	public: 
		progress_bar(); ///< 类默认的构造函数。

		/**
		 * @brief      类的构造函数。
		 *
		 * @param[in]  n_            整形，进度条循环的最大值。
		 * @param[in]  description_  字符串，进度条的名称。
		 * @param      out_          输出至标准错误输出，避免在程序输出重定向时无法显示。
		 */
		progress_bar(unsigned long n_, const char *description_="Progress", std::ostream& out_=std::cerr);
		 /**
		 * @brief      初始化进度条，与类的构造函数功能一样。可以重置或者初始化进度条。
		 *
		 * @param[in]  n_            整形，进度条循环的最大值。
		 * @param[in]  description_  字符串，进度条的名称。
		 * @param      out_          输出至标准错误输出，避免在程序输出重定向时无法显示。
		 */
		void reset(unsigned long n_, const char *description_="Progress", std::ostream& out_=std::cerr);
		/**
		 * @brief      设置进度条刷新次数，默认为进度条的长度，即每次都更新。减少更新的次数可适当的减少在屏幕上打印进度条的时间
		 *
		 * @param[in]  frequency_update_  整形，刷新的次数
		 */
		void set_frequency_update(unsigned long frequency_update_);
		/**
		 * @brief      设置进度条的风格。
		 *
		 * @param[in]  unit_bar_    字符串，已达到进度的符号。
		 * @param[in]  unit_space_  字符串，未达到进度的符号。
		 */
		void set_style(const char* unit_bar_, const char* unit_space_);
		/**
		 * @brief      更新当前已达到的进度值。
		 *
		 * @param[in]  idx_  整形，当前进度条循环的值。
		 */
		void progressed(unsigned long idx_);
		/**
		 * @brief      更新当前已达到的进度值。仅显示百分比
		 *
		 * @param[in]  idx_  整形，当前进度条循环的值。
		 */
		void progressed_simple(unsigned long idx_);

	protected:
		unsigned long n;
		unsigned int desc_width;
		unsigned long frequency_update;
		std::ostream* out;

		const char *description;
		const char *unit_bar;
		const char *unit_space;

		void clear_bar_field();
		int get_console_width();
		int get_bar_length();
	};
}

#endif // _GCTL_PROGRESS_BAR_H